package com.wanlian.router.ui.router;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Switch;

import com.smart.android.ui.BaseFragment;
import com.smart.android.widget.EditTextWithClear;
import com.wanlian.router.R;
import com.wanlian.router.bean.WifiSetting;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Hyu on 2018/10/25.
 * Email: fvaryu@163.com
 */
public class WifiSettingBindFragment extends BaseFragment implements TextWatcher {
    private static final String[] TYPES = new String[]{"WPA2", "WPA/WPA2", "无密码"};
    private static final String[] TYPES_Value = new String[]{"psk2", "psk-mixed", "none"};// none（不加密）/psk-mixed（WPA/WPA2混合加密）/psk2（WPA2加密）
    @BindView(R.id.rg_ency)
    RadioGroup rg_ency;
    @BindView(R.id.et_wifi_ssid)
    EditTextWithClear et_wifi_ssid;
    @BindView(R.id.et_wifi_pwd)
    EditTextWithClear et_wifi_pwd;
    @BindView(R.id.switch_none)
    Switch switch_none;
    @BindView(R.id.image_check_1)
    ImageView image_check_1;
    private WifiSetting mWifiSetting;
    private int wifiType;
    private boolean showPwd, showPwd1;
    private OnSettingListener mOnSettingListener;

    public static WifiSettingBindFragment newInstance(int type, WifiSetting wifiSetting) {

        Bundle args = new Bundle();
        args.putInt("type", type);
        args.putSerializable("wifiset", wifiSetting);
        WifiSettingBindFragment fragment = new WifiSettingBindFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public void setOnSettingListener(OnSettingListener onSettingListener) {
        mOnSettingListener = onSettingListener;
    }

    @Override
    protected int layout() {
        return R.layout.fragment_wifi_setting_bind;
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);

        wifiType = getArguments().getInt("type");
        mWifiSetting = (WifiSetting) getArguments().getSerializable("wifiset");
    }

    @Override
    protected void initData(View view) {
        super.initData(view);
        et_wifi_ssid.addTextChangedListener(this);
        et_wifi_pwd.addTextChangedListener(this);



        rg_ency.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                next();
            }
        });

        rg_ency.check(R.id.rb_wpa);
    }

    public int checkId() {
        switch (rg_ency.getCheckedRadioButtonId()) {
            case R.id.rb_wpa:
                return 0;
            case R.id.rb_wpa_2:
                return 1;
            case R.id.rb_none:
                return 2;
        }
        return 0;
    }

    @OnClick(R.id.image_check_1)
    void check1() {
        showPwd1 = !showPwd1;
        if (showPwd1) {
            image_check_1.setImageResource(R.drawable.eye_open_gray);
            et_wifi_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            image_check_1.setImageResource(R.drawable.eye_close_gray);
            et_wifi_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

        next();


    }

    private void next() {
        if (mOnSettingListener != null) {
            switch (wifiType) {
                case 0: {
                    mWifiSetting.setHastwoConfig(true);
                    mWifiSetting.setTwoWifiAccount(et_wifi_ssid.getText().toString());
                    mWifiSetting.setTwoWifiPassWord(et_wifi_pwd.getText().toString());
                    mWifiSetting.setTwoWifiForce(switch_none.isChecked());

                    mWifiSetting.setTwoWifiLocktype(TYPES_Value[checkId()]);

                }
                break;
                case 1: {
                    mWifiSetting.setHasFiveConfig(true);
                    mWifiSetting.setFiveAccount(et_wifi_ssid.getText().toString());
                    mWifiSetting.setFiveWifiPassWord(et_wifi_pwd.getText().toString());
                    mWifiSetting.setFiveWifiForce(switch_none.isChecked());
                    mWifiSetting.setFiveWifiLocktype(TYPES_Value[checkId()]);
                }
                break;
                case 2: {
                    mWifiSetting.setHasCustomerConfig(true);
                    mWifiSetting.setCustomerWifiAccount(et_wifi_ssid.getText().toString());
                    mWifiSetting.setCustomerWifiPassWord(et_wifi_pwd.getText().toString());
                    mWifiSetting.setCustomerWifiForce(switch_none.isChecked());
                    mWifiSetting.setCustomerWifiLocktype(TYPES_Value[checkId()]);
                }
                break;
            }
            mOnSettingListener.onSetting(wifiType, mWifiSetting);
        }
    }

    public interface OnSettingListener {
        void onSetting(int type, WifiSetting wifiSetting);
    }
}
